package com.bdc.whatwhat.views.cell;

import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;

import com.bdc.whatwhat.R;
import com.bdc.whatwhat.modele.WhatWhat;
import com.bdc.whatwhat.session.SessionManager;
import com.bdc.whatwhat.task.AimerTask;
import com.bdc.whatwhat.task.FaireWhatWhatTask;
import com.bdc.whatwhat.utils.ImageUtils;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

/**
 * Created by gfimac on 21/07/2014.
 */
public class WhatWhatCell extends ViewCell<WhatWhat> implements View.OnClickListener, AimerTask.AimerTaskDelegate {

    TextView nombre;
    TextView nombreAimer;
    TextView texte;
    TextView auteur;

    View content;

    View actions;
    View definir;
    View annuler;

    View faire;
    View share;
    View like;

    View whatwhat_like_on;
    AimerTask aimerTask;

    FaireWhatWhatTask.FaireWhatWhatTaskDelegate faireWhatWhatTaskDelegate;
    SessionManager sessionManager;


    @Override
    public void animer() {
    }

    @Override
    public void construire() {
        sessionManager = new SessionManager(getContext());
        if (getParams() != null) {
            if (getParams().length >= 1) {
                faireWhatWhatTaskDelegate = (FaireWhatWhatTask.FaireWhatWhatTaskDelegate) getParams()[0];
            }
        }
    }

    @Override
    public void charger() {
        nombre = (TextView) findViewById(R.id.whatwhat_nombre);
        nombreAimer = (TextView) findViewById(R.id.whatwhat_nombre_like);
        texte = (TextView) findViewById(R.id.whatwhat_texte);
        auteur = (TextView) findViewById(R.id.whatwhat_auteur);

        faire = findViewById(R.id.whatwhat_faire);
        share = findViewById(R.id.whatwhat_share);
        like = findViewById(R.id.whatwhat_like);

        actions = findViewById(R.id.whatwhat_actions);
        definir = findViewById(R.id.whatwhat_definir);
        annuler = findViewById(R.id.whatwhat_annuler);

        content = findViewById(R.id.whatwhat_content);

        whatwhat_like_on = findViewById(R.id.whatwhat_like_on);
    }

    @Override
    public void remplir() {
        getView().setId(position);

        if (getObject() != null) {
            if (getObject().getNombre() != null)
                nombre.setText(getObject().getNombre().toString());
            if (getObject().getTexte() != null)
                texte.setText("“"+getObject().getTexte()+"”");
            if (getObject().getCreateur() != null && getObject().getCreateur().getNom() != null && getObject().getCreateur().getPrenom() != null)
                auteur.setText(getObject().getCreateur().getPrenom() + " " + getObject().getCreateur().getNom());

            if (getObject().isAimer())
                whatwhat_like_on.setVisibility(View.VISIBLE);
            else
                whatwhat_like_on.setVisibility(View.GONE);

            if(getObject().getNombreAimer() != null && !getObject().getNombreAimer().equals("0")){
                nombreAimer.setText(getObject().getNombreAimer());
                findViewById(R.id.whatwhat_nombre_like_layout).setVisibility(View.VISIBLE);
            }else{
                findViewById(R.id.whatwhat_nombre_like_layout).setVisibility(View.GONE);
            }
        }

        actions.setVisibility(View.GONE);

    }

    public FaireWhatWhatTask.FaireWhatWhatTaskDelegate getFaireWhatWhatTaskDelegate() {
        return faireWhatWhatTaskDelegate;
    }

    public void setFaireWhatWhatTaskDelegate(FaireWhatWhatTask.FaireWhatWhatTaskDelegate faireWhatWhatTaskDelegate) {
        this.faireWhatWhatTaskDelegate = faireWhatWhatTaskDelegate;
    }

    @Override
    public void ajouterListeners() {
        //findViewById(R.id.content).setOnClickListener(this);

        definir.setOnClickListener(this);
        annuler.setOnClickListener(this);

        faire.setOnClickListener(this);
        share.setOnClickListener(this);
        like.setOnClickListener(this);
        whatwhat_like_on.setOnClickListener(this);
        texte.setOnClickListener(this);

        content.setOnClickListener(this);
    }

    public void afficherActions() {

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(500);
        animatorSet.setInterpolator(new DecelerateInterpolator());
        animatorSet.playTogether(
                ObjectAnimator.ofFloat(actions, "alpha", 0, 1)
        );
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                actions.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.start();

    }

    public void cacherActions() {
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(500);
        animatorSet.setInterpolator(new DecelerateInterpolator());
        animatorSet.playTogether(
                ObjectAnimator.ofFloat(actions, "alpha", 1, 0)
        );
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                actions.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.start();
    }

    public void aimer() {

        Log.d(getClass().toString(), "aimer "+getView().getId());

        getObject().setAimer(!getObject().isAimer());

        float max = 1.2f;

        final AnimatorSet anim = new AnimatorSet().setDuration(800);
        anim.setInterpolator(new AccelerateInterpolator());
        if (getObject().isAimer()) {
            anim.playTogether(
                    ObjectAnimator.ofFloat(whatwhat_like_on, "alpha", 0, 1),
                    ObjectAnimator.ofFloat(whatwhat_like_on, "scaleX", 0, max, 1),
                    ObjectAnimator.ofFloat(whatwhat_like_on, "scaleY", 0, max, 1)
            );
        } else {
            anim.playTogether(
                    ObjectAnimator.ofFloat(whatwhat_like_on, "alpha", 1, 0),
                    ObjectAnimator.ofFloat(whatwhat_like_on, "scaleX", 1, max, 0),
                    ObjectAnimator.ofFloat(whatwhat_like_on, "scaleY", 1, max, 0)
            );
        }

        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                if(getObject().isAimer())
                    whatwhat_like_on.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {

                if(!getObject().isAimer())
                    whatwhat_like_on.setVisibility(View.GONE);

                if (aimerTask != null)
                    aimerTask.cancel(true);

                aimerTask = new AimerTask(WhatWhatCell.this);
                aimerTask.execute(getObject().getId(), sessionManager.getToken());
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        anim.start();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.whatwhat_share:
                ImageUtils.sendImageShare(getContext(), getView());
                break;
            case R.id.whatwhat_definir:
                if (faireWhatWhatTaskDelegate != null)
                    faireWhatWhatTaskDelegate.faire(getObject());
                cacherActions();
                break;
            case R.id.whatwhat_annuler:
                cacherActions();
                break;
            case R.id.whatwhat_like:
            case R.id.whatwhat_like_on:
                aimer();
                break;
            case R.id.whatwhat_content:
            case R.id.whatwhat_texte:
            case R.id.whatwhat_faire:
                afficherActions();
                break;
        }

    }


    @Override
    public void afficherChargement(boolean afficher) {

    }

    @Override
    public void onAimerEnvoye() {

    }

    @Override
    public void onErreurReseau() {

    }
}
